<?php
set_time_limit(1200);
error_reporting(0);
$PurviewLevel=0;

require("../../inc/common.inc.php");
require_once(dirname(__FILE__)."/../"."Admin_ChkPurview.php");

?>
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>微信公众平台</title>
    <style>
      html{position:absolute;width:100%;height:26px;display:block;overflow:hidden;}
	  body, #uploadForm{overflow:hidden;margin:0;padding:0;position:absolute;top:0;right:0;z-index:1000;width:100%;height:32px;}
	  .file-wrapper{ cursor: pointer; display: block; overflow: hidden; position: relative; }
	  .file-wrapper input{ cursor: pointer; position: absolute; right: 0; top: 0; height:32px;width:100%;}
	  .file-wrapper .button{ cursor: pointer; display:block; width:100%; height:32px;  }
    </style>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body class=" zh_CN">
  
  <?php
if($uploadfile){
	if( !ereg("^[0-9a-z_]+$",$dir) ){
		$dir="other";
	}
	//$updir='';
	$path="$dir/".date("Ym");
	$array[name]=is_array($uploadfile)?$_FILES[uploadfile][name]:$uploadfile_name;
	$array[path]=$webdb[updir]."/".$path;
	$array[size]=is_array($uploadfile)?$_FILES[uploadfile][size]:$uploadfile_size;
	
	if($type==1||$type==2){
		if($array[size]>2*1024*1024){
			echo '<script>alert("文件大小超过上限");history.go(-1);</script>';
			exit;
		}
	}elseif($type==3){
		if($array[size]>5*1024*1024){
			echo '<script>alert("文件大小超过上限");history.go(-1);</script>';
			exit;
		}
	}elseif($type==4){
		if($array[size]>20*1024*1024){
			echo '<script>alert("文件大小超过上限");history.go(-1);</script>';
			exit;
		}
	}
	$filename=upfile(is_array($uploadfile)?$_FILES[uploadfile][tmp_name]:$uploadfile,$array);
    if(!$filename){
		echo '<script>alert("文件上传失败,请再次上传，如果多次失败，请联系管理员");</script>';
	}else {
		//存入数据库
		$newfile="$array[path]/$filename";
		$fn || $fn="upfile";
		if($fn=="showimg"){
		echo "
			<script>
			window.top.main.$fn('$newfile','$newfile','$label');
			</script>
			";
		}else{
		echo "
			<script>alert('上传成功');</script>
			<script>
			window.top.main.$fn('$img_id');
			</script>
			";	
		}
	}
}
?>
	<form id="uploadForm" method="POST" enctype="multipart/form-data" action="">
	  <span class="file-wrapper">
		<input id="uploadFile" name="uploadfile" class="uploadFile" type="file" onchange="upload()" />
		<span class="button"></span>
	  </span>
      <input name="formId" class="formId" val="" type="hidden" />
      <input type="hidden" name="action" value="uploadfile">
    </form>
    <script type="text/javascript" src="../js/jquery-1.7.2.min17e4f5.js"></script>
    <script type="text/javascript">
	
      
      var 
        url = window.location.href,
        type = url.match(/[\?&]type=([^&]*)/),
        formId = url.match(/[\?&]formId=([^&]*)/);
      cstype = type && type[1] || 0;
	  if(cstype==1){type=2}else{type=cstype}
      formId = formId && formId[1];
      var _allowType = {
        ".bmp":2, ".png":2, ".jpeg":2, ".jpg":2, ".gif":2,
        ".mp3":3, ".wma":3, ".wav":3, ".amr":3,
        ".flv":4
      };
      var errMsg = {
        "0" : '图片格式必须为以下格式：bmp, png, jpeg, jpg, gif<br/>'
              + '语音格式必须为以下格式：mp3, wma, wav, amr<br/>'
              + '视频格式必须为以下格式：rm, rmvb, wmv, avi, mpg, mpeg, mp4<br/>',
        "2" : '图片格式必须为以下格式：bmp, png, jpeg, jpg, gif',
        "3" : '语音格式必须为以下格式：mp3, wma, wav, amr',
        "4" : '视频格式必须为以下格式：rm, rmvb, wmv, avi, mpg, mpeg, mp4'
      }
      function _checkFileType(_fileName, _type){
        var _ext = _fileName.substr(_fileName.lastIndexOf(".")).toLowerCase(),
          _res = _allowType[_ext];
        
        if (_type == 0) {
          return !!_res;
        } else {
          return _type == _res;
        }
      }
      function upload(){
		  
        var _fileName = jQuery.trim(jQuery('#uploadFile').val());
        if (!_fileName || _fileName == ""){  // 提交前的再次检测
          alert('请选择文件上传');
          return false;
        }
        if (!_checkFileType(_fileName, type)){
		  alert('上传文件格式错误');
          return false;
        }
        jQuery('#uploadForm').submit();
      }
    </script>
  </body>
</html>
